import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;

public class MyTray {

	MyTray(){

		final TrayIcon trayIcon;

		if (SystemTray.isSupported()) {

			SystemTray tray = SystemTray.getSystemTray();
			Image image = Toolkit.getDefaultToolkit().getImage("tray.gif");

			MouseListener mouseListener = new MouseListener() {

				public void mouseClicked(MouseEvent e) {
					//  System.out.println("Tray Icon - Mouse clicked!");                 
				}

				public void mouseEntered(MouseEvent e) {
					// System.out.println("Tray Icon - Mouse entered!");                 
				}

				public void mouseExited(MouseEvent e) {
					// System.out.println("Tray Icon - Mouse exited!");                 
				}

				public void mousePressed(MouseEvent e) {
					//  System.out.println("Tray Icon - Mouse pressed!");                 
				}

				public void mouseReleased(MouseEvent e) {
					//  System.out.println("Tray Icon - Mouse released!");                 
				}
			};

			ActionListener ExitListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					//System.out.println("Exiting...");
					System.exit(0);
				}
			};


			ActionListener CheckMailListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						Gmail.CheckMail();
						MailInfoWindow.Show();
					} catch (Exception e1) {
						e1.printStackTrace();
					}

				}
			};


			ActionListener SettingsListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						new MailSettingsForm();
						//MailSettingsForm.Show();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			};


			PopupMenu popup = new PopupMenu();
			MenuItem CheckMailItem = new MenuItem("Check Mail Now");
			MenuItem SettingsItem = new MenuItem("Settings");
			MenuItem ExitItem = new MenuItem("Exit");

			CheckMailItem.addActionListener(CheckMailListener);
			SettingsItem.addActionListener(SettingsListener);
			ExitItem.addActionListener(ExitListener);

			popup.add(CheckMailItem);
			popup.add(SettingsItem);
			popup.add(ExitItem);


			trayIcon = new TrayIcon(image, "Mail on Tray", popup);

			ActionListener actionListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					trayIcon.displayMessage("MailOnTray", 
							"You got " + Gmail.NumberOfEmails + " mail!",
							TrayIcon.MessageType.INFO);
				}
			};

			trayIcon.setImageAutoSize(true);
			trayIcon.addActionListener(actionListener);
			trayIcon.addMouseListener(mouseListener);

			try {
				tray.add(trayIcon);
			} catch (AWTException e) {
				System.err.println("TrayIcon could not be added.");
			}

		} else {

			//  System Tray is not supported

		}



	}
}
